<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10">

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="院子" prop="title">
            <el-select v-model="form.orgIds">
              <el-option
                v-for="item in orgList"
                :key="item.id"
                :label="item.orgName"
                :value="item.orgIds">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="人数" prop="number">
            <el-input v-model="form.number" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="form.date"
              placeholder="选择日期"
              style="width: 100%"
              type="date"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xl="12" :xs="12">
          <el-form-item label="封面图" prop="json">
            <UploadImage ref="UploadImage" :imgageUrl="form.imgs" @returnImgUrl="returnImgUrl"></UploadImage>
          </el-form-item>
        </el-col>
        <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <el-form-item label="图文详情" prop="json">
            <VabQuill
              ref="QuillEditor"
              v-model="form.json"
              :min-height="300"
              style="width: 100%;"
            ></VabQuill>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doInsert, doUpdate} from "@/api/shop/previousevents/ShopPreviousEventsManagementApi";
import {isNull} from "@/utils/validate";
import {formateDate} from "@/utils/format";
import {uploadJsonFile} from "@/api/commom";
import 'zx-quill/dist/zx-quill.css'

import VabQuill from 'zx-quill'
import UploadImage from "@/views/modules/common/UploadImage";
import {getOrgAll} from "@/utils/cookies";
import {getJson} from "@/api/tool/ossTool";

export default {
  components: {
    VabQuill, UploadImage
  },
  name: "ShopPreviousEventsManagementEdit",
  data() {


    return {
      orgList: [],
      form: {
        // 设置默认值
        version: 0
      },
      dict: {},
      rules: {
        title: [
          {required: true, trigger: "blur", message: "标题非空"},
        ],
        number: [
          {required: true, trigger: "blur", message: "人数非空"},
        ],
        date: [
          {required: true, trigger: "blur", message: "日期非空"},
        ],
        json: [
          {required: true, trigger: "blur", message: "图文详情非空"},
        ],
      },
      title: "",
      dialogFormVisible: false,
    };
  },
  created() {
    this.getOrgAll()
  },
  mounted() {
    // 加载字典值
  },
  methods: {
    getOrgAll(id) {
      this.orgList = getOrgAll(id);
    },
    returnImgUrl(e, i) {
      this.form.imgs = e
    },
    showEdit(row) {
      if (!row) {
        this.title = "添加";
      } else {
        this.title = "编辑";
        this.form = Object.assign({}, row);
        this.getJson(this.form.json)
      }
      this.dialogFormVisible = true;
    },
    async getJson(url) {
      const {data} = await getJson(url);
      this.form.json = data

    },
    close() {
      this.dialogFormVisible = false;
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 处理数据
          this.handlerFormData(this.form);

          if (!isNull(this.form.json)) {
            let {data} = await uploadJsonFile("json", this.form.json);
            this.form.json = data.fileStoragePath
          }
          // 修改
          if (!isNull(this.form.id)) {
            const {msg} = await doUpdate(this.form);
            this.$baseMessage(msg, "success");
          } else {
            const {msg} = await doInsert(this.form);
            this.$baseMessage(msg, "success");
          }

          await this.$emit("fetchData");
          this.close();
        } else {
          return false;
        }
      });
    },
    // 处理 form数据
    handlerFormData(formData) {
      if (!isNull(formData)) {
        for (let key in formData) {
          // 对于时间类进行处理
          if ("[object Date]" === Object.prototype.toString.call(formData[key])) {
            formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
          }
        }
      }
    },
  },
};
</script>
